package com.dw.article.controller;



import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.dw.article.mapper.ee.EeArticleInfoMapper;
import com.dw.article.service.ArticleInfoService;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.utils.ResultUtils;
import com.dw.model.article.dtos.user.AddArticleUserDTO;
import com.dw.article.service.ArticleUserService;
import com.dw.model.article.entitys.ArticleInfo;
import com.dw.model.article.vos.article.GetCountVO;
import com.dw.model.article.vos.article.QueryUserArticleVO;
import com.dw.model.article.vos.article.SearchArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章用户关系表(ArticleUser)表控制层
 *
 * @author makejava
 * @since 2024-10-09 20:12:53
 */
@Slf4j
@RestController
@RequestMapping("/article/user")
public class ArticleUserController {

    @Resource
    private ArticleUserService articleUserService;

    @Resource
    private ArticleInfoService articleInfoService;

    @Resource
    private EeArticleInfoMapper eeArticleInfoMapper;

    /**
     * 点赞
     */
    @PostMapping("/like")
    public BaseResponse<Boolean> like(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("点赞参数:{}",addArticleUserDTO);
        articleUserService.like(addArticleUserDTO);
        return ResultUtils.success(true);
    }

    /**
     * 收藏
     */
    @PostMapping("/star")
    public BaseResponse<Boolean> star(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("收藏参数:{}",addArticleUserDTO);
        articleUserService.star(addArticleUserDTO);
        return ResultUtils.success(true);
    }

    /**
     * 转发
     */
    @PostMapping("/share")
    public BaseResponse<Boolean> share(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("转发参数:{}",addArticleUserDTO);
        articleUserService.share(addArticleUserDTO);
        return ResultUtils.success(true);
    }

    /**
     * 获取文章点赞收藏转发数量
     */
    @GetMapping("/getCount")
    public BaseResponse<GetCountVO> getArticleUserCount(@RequestParam("articleId") Long articleId){
        log.info("获取文章点赞收藏转发数量参数:{}",articleId);
        GetCountVO getCountVO = articleUserService.getArticleUserCount(articleId);
        return ResultUtils.success(getCountVO);
    }

    /**
     * 查询当前用户是否点赞收藏转发
     */
    @GetMapping("/queryUserArticle")
    public BaseResponse<QueryUserArticleVO> queryUserArticle(@RequestParam("articleId") Long articleId){
        log.info("查询当前用户是否点赞收藏转发参数:{}",articleId);
        QueryUserArticleVO queryUserArticle = articleUserService.queryUserArticle(articleId);
        return ResultUtils.success(queryUserArticle);
    }

    /**
     * 查询用户点赞的文章
     */
    @PostMapping("/getUserLiked")
    public BaseResponse<List<SearchArticleVO>> getMyLiked(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("查询我的点赞参数:{}",addArticleUserDTO);
        List<Long> ids = articleUserService.getMyLiked(addArticleUserDTO);
        // 条件查询
        LambdaEsQueryWrapper<ArticleInfo> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.in(ArticleInfo::getLeiId,ids)
                .orderByDesc(ArticleInfo::getCreatedTime);
        List<ArticleInfo> articleInfoList = eeArticleInfoMapper.selectList(wrapper);
        // 分页查询
        List<List<ArticleInfo>> partition = Lists.partition(articleInfoList, addArticleUserDTO.getPageSize());
        List<ArticleInfo> articleInfos = partition.get(addArticleUserDTO.getCurrent() - 1);
        // 终极查询！！！
        List<SearchArticleVO> result = articleInfoService.queryArticleInfo(articleInfos);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户收藏的文章
     */
    @PostMapping("/getUserStar")
    public BaseResponse<List<SearchArticleVO>> getMyStar(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("查询我的收藏参数:{}",addArticleUserDTO);
        List<Long> ids = articleUserService.getMyStar(addArticleUserDTO);
        // 条件查询
        LambdaEsQueryWrapper<ArticleInfo> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.in(ArticleInfo::getLeiId,ids)
                .orderByDesc(ArticleInfo::getCreatedTime);
        List<ArticleInfo> articleInfoList = eeArticleInfoMapper.selectList(wrapper);
        // 分页查询
        List<List<ArticleInfo>> partition = Lists.partition(articleInfoList, addArticleUserDTO.getPageSize());
        List<ArticleInfo> articleInfos = partition.get(addArticleUserDTO.getCurrent() - 1);
        // 终极查询！！！
        List<SearchArticleVO> result = articleInfoService.queryArticleInfo(articleInfos);
        return ResultUtils.success(result);
    }

    /**
     * 查询用户分享的文章
     */
    @PostMapping("/getUserShared")
    public BaseResponse<List<SearchArticleVO>> getMyShared(@RequestBody AddArticleUserDTO addArticleUserDTO){
        log.info("查询我的分享参数:{}",addArticleUserDTO);
        List<Long> ids = articleUserService.getMyShared(addArticleUserDTO);
        // 条件查询
        LambdaEsQueryWrapper<ArticleInfo> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.in(ArticleInfo::getLeiId,ids)
                .orderByDesc(ArticleInfo::getCreatedTime);
        List<ArticleInfo> articleInfoList = eeArticleInfoMapper.selectList(wrapper);
        // 分页查询
        List<List<ArticleInfo>> partition = Lists.partition(articleInfoList, addArticleUserDTO.getPageSize());
        List<ArticleInfo> articleInfos = partition.get(addArticleUserDTO.getCurrent() - 1);
        // 终极查询！！！
        List<SearchArticleVO> result = articleInfoService.queryArticleInfo(articleInfos);
        return ResultUtils.success(result);
    }

}

